package com.yuanlp.mobileplayer.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.yuanlp.mobileplayer.bean.Lyrc;
import com.yuanlp.mobileplayer.utils.DensityUtil;

import java.util.ArrayList;

/**
 * Created by 原立鹏 on 2017/7/30.
 *
 * 自定义歌词显示控件
 */

public class ShowlyrcView extends android.support.v7.widget.AppCompatTextView {
    private ArrayList<Lyrc> lyrcs;
    private Paint paint;  //当前显示的歌词的画笔
    private Paint whitepaint;  //白色画笔，用来绘制不是当前高亮的部分

    private int width;  //控件的宽
    private int height;  //控件的高

    private int index;  //当前歌词的索引
    private int textHeight;  //每行歌词的高度
    private int currentPosition;  //当前播放进度

    private long sleepTime;  //高亮显示时间
    private long timePosition;  //当前歌词的时间戳

    /**
     * 设置歌词列表
     * @param lyrcs
     */
    public void setLyrcs(ArrayList<Lyrc> lyrcs){
        this.lyrcs=lyrcs;
    }

    public ShowlyrcView(Context context) {
        this(context,null);
    }

    public ShowlyrcView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShowlyrcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width=w;
        height=h;
    }

    private void initView(Context context) {
        textHeight= DensityUtil.dip2px(context,20);  //转换为对应的像素

        //创建画笔----当前高亮的画笔
        paint=new Paint();
        paint.setColor(Color.GREEN); //高亮颜色
        paint.setTextSize(DensityUtil.dip2px(context,20));
        paint.setAntiAlias(true); //抗锯齿
        paint.setTextAlign(Paint.Align.CENTER);  //对齐方式,居中显示

        //白色画笔
        whitepaint=new Paint();
        whitepaint.setColor(Color.WHITE);
        whitepaint.setTextAlign(Paint.Align.CENTER);
        whitepaint.setTextSize(DensityUtil.dip2px(context,20));
        whitepaint.setAntiAlias(true);

        /**
         * 暂时先设置一个假的歌词列表
         */
        lyrcs=new ArrayList<>();


//        for (int i=0;i<1000;i++){
//            Lyrc lyrc=new Lyrc();
//
//            lyrc.setTimePosition(i*1000);
//            lyrc.setContent("这是一句歌词"+i);
//            lyrc.setSleepTime(1500+i);
//
//            lyrcs.add(lyrc);
//        }
    }


    //绘制歌词
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (lyrcs!=null&&lyrcs.size()>0){
            //先绘制当前歌词
            String currentContent=lyrcs.get(index).getContent();
            canvas.drawText(currentContent,getWidth()/2,getHeight()/2,paint);

            //绘制前面部分歌词
            float tempY=getHeight()/2;  //当前高亮歌词的Y轴坐标
            for (int i=index-1;i>=0;i--){
                String preContent=lyrcs.get(i).getContent();
                tempY=tempY-textHeight;

                if (tempY<0){  //当最上面的一行歌词已经隐藏了，不显示，那么就不再处理
                    break;
                }
                canvas.drawText(preContent,getWidth()/2,tempY,whitepaint);
            }

            //绘制后面部分的歌词
            tempY=getHeight()/2;
            for (int i=index+1;i<lyrcs.size();i++){
                String nextContent=lyrcs.get(i).getContent();
                tempY=tempY+textHeight;
                if (tempY>getHeight()){  //超出控件的高度，就不处理
                    break;
                }
                canvas.drawText(nextContent,getWidth()/2,tempY,whitepaint);
            }

        }else {
            canvas.drawText("没有歌词",getWidth()/2,getHeight()/2,paint);
        }
    }

    public void setShowNextLyrc(int currentPosition) {
        this.currentPosition=currentPosition;
        if (lyrcs==null&&lyrcs.size()==0){
            return;
        }else{
            for (int i=1;i<lyrcs.size();i++){
                if (currentPosition<lyrcs.get(i).getTimePosition()){
                    int tempIndex=i-1;
                    if (currentPosition>=lyrcs.get(tempIndex).getTimePosition()){
                        //当前正在播放的歌词
                        index=tempIndex;
                        sleepTime = lyrcs.get(index).getSleepTime();
                        timePosition = lyrcs.get(index).getTimePosition();
                    }
                }
            }
        }

        //重新绘制，在主线程执行
        invalidate();
    }
}
